/**
 * Collection Upload Page JavaScript
 * Handles file uploads for a specific collection
 */

// Use existing URLS configuration from config/urls.js
// Collection upload endpoint is now available as URLS.LIBRARY_API.COLLECTION_UPLOAD

// Safe fetch wrapper with URL validation
async function safeFetch(url, options = {}) {
    // Internal URLs starting with '/' are safe
    if (!url.startsWith('/')) {
        if (!URLValidator.isSafeUrl(url)) {
            throw new Error(`Blocked unsafe URL: ${url}`);
        }
    }
    return fetch(url, options);
}

/**
 * Escape HTML special characters to prevent XSS attacks
 * @param {string} text - Text to escape
 * @returns {string} - Escaped text safe for HTML insertion
 */
function escapeHtml(text) {
    const div = document.createElement('div');
    div.textContent = text;
    return div.innerHTML;
}

// Store selected files globally to avoid losing them
let selectedFiles = [];

/**
 * Initialize the upload page
 */
document.addEventListener('DOMContentLoaded', function() {
    // Setup form submission
    document.getElementById('upload-files-form').addEventListener('submit', function(e) {
        console.log('📋 Form submit event triggered');
        console.log('📋 File input state before submit:');
        const fileInput = document.getElementById('files-input');
        console.log('📋 - files:', fileInput.files);
        console.log('📋 - value:', fileInput.value);
        console.log('📋 - length:', fileInput.files.length);
        handleUploadFiles(e);
    });

    // Setup file input change handler
    document.getElementById('files-input').addEventListener('change', handleFileSelect);

    // Setup drag and drop
    const dropZone = document.getElementById('drop-zone');
    const fileInput = document.getElementById('files-input');

    if (dropZone && fileInput) {
        dropZone.addEventListener('click', (e) => {
            if (e.target.tagName !== 'BUTTON') {
                fileInput.click();
            }
        });

        dropZone.addEventListener('dragover', handleDragOver);
        dropZone.addEventListener('dragleave', handleDragLeave);
        dropZone.addEventListener('drop', handleDrop);
    }

    // IMMEDIATE document-level drag detection
    // Fires instantly when ANY file drag enters the window
    let dragCounter = 0;

    document.addEventListener('dragenter', (e) => {
        e.preventDefault();
        dragCounter++;
        if (e.dataTransfer && e.dataTransfer.types.includes('Files')) {
            const dropZone = document.getElementById('drop-zone');
            if (dropZone) {
                dropZone.classList.add('ldr-drag-over');
                // Scroll drop zone into view if not visible
                if (!isElementInViewport(dropZone)) {
                    dropZone.scrollIntoView({ behavior: 'smooth', block: 'center' });
                }
            }
        }
    }, { capture: true });

    document.addEventListener('dragleave', (e) => {
        e.preventDefault();
        dragCounter--;
        // Only remove class when truly leaving the document
        if (dragCounter <= 0) {
            dragCounter = 0;
            const dropZone = document.getElementById('drop-zone');
            if (dropZone) {
                dropZone.classList.remove('ldr-drag-over');
            }
        }
    }, { capture: true });

    document.addEventListener('drop', (e) => {
        dragCounter = 0;
        const dropZone = document.getElementById('drop-zone');
        if (dropZone) {
            dropZone.classList.remove('ldr-drag-over');
        }
    }, { capture: true });

    // Prevent default for dragover on document to allow drop
    document.addEventListener('dragover', (e) => {
        e.preventDefault();
    }, { capture: true });
});

/**
 * Check if element is in viewport
 */
function isElementInViewport(el) {
    const rect = el.getBoundingClientRect();
    return (
        rect.top >= 0 &&
        rect.left >= 0 &&
        rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
        rect.right <= (window.innerWidth || document.documentElement.clientWidth)
    );
}

// Throttle drag events for better performance
let dragOverTimeout = null;

/**
 * Handle drag over event (throttled)
 */
function handleDragOver(e) {
    e.preventDefault();
    e.stopPropagation();

    // Only update class if not already set (avoid repeated DOM updates)
    if (!e.currentTarget.classList.contains('ldr-drag-over')) {
        e.currentTarget.classList.add('ldr-drag-over');
    }

    // Clear any pending drag leave
    if (dragOverTimeout) {
        clearTimeout(dragOverTimeout);
        dragOverTimeout = null;
    }
}

/**
 * Handle drag leave event (debounced)
 */
function handleDragLeave(e) {
    e.preventDefault();
    e.stopPropagation();

    const target = e.currentTarget;

    // Debounce the class removal to avoid flickering
    dragOverTimeout = setTimeout(() => {
        target.classList.remove('ldr-drag-over');
    }, 50);
}

/**
 * Handle drop event
 */
function handleDrop(e) {
    e.preventDefault();
    e.stopPropagation();

    const dropZone = e.currentTarget;
    dropZone.classList.remove('ldr-drag-over');

    const files = e.dataTransfer.files;
    handleFiles(files);
}

/**
 * Handle file selection
 */
function handleFileSelect(e) {
    console.log('📂 File selection triggered');
    console.log('📂 Event target:', e.target);
    console.log('📂 Files from event:', e.target.files);
    const files = e.target.files;
    console.log('📂 Calling handleFiles with:', files.length, 'files');
    handleFiles(files);
}

/**
 * Handle selected files
 */
function handleFiles(files) {
    console.log('📦 handleFiles called with:', files.length, 'files');

    // Store files globally
    selectedFiles = Array.from(files);
    console.log('📦 Stored files globally:', selectedFiles.length, 'files');

    if (files.length === 0) {
        hideSelectedFiles();
        return;
    }

    showSelectedFiles(files);
}

/**
 * Show selected files preview
 */
function showSelectedFiles(files) {
    console.log(`👀 Showing preview for ${files.length} files`);
    const selectedFilesDiv = document.getElementById('selected-files');
    const fileList = document.getElementById('file-list');

    // Clear existing file list
    fileList.innerHTML = '';

    // Add each file to list (batch DOM updates)
    const fragment = document.createDocumentFragment();
    Array.from(files).forEach((file) => {
        const li = document.createElement('li');
        const fileSize = (file.size / 1024 / 1024).toFixed(2) + ' MB';
        li.innerHTML = `
            <i class="fas fa-file"></i>
            <span>${escapeHtml(file.name)} (${fileSize})</span>
        `;
        fragment.appendChild(li);
    });
    fileList.appendChild(fragment);

    selectedFilesDiv.style.display = 'block';
}

/**
 * Hide selected files preview
 */
function hideSelectedFiles() {
    const selectedFilesDiv = document.getElementById('selected-files');
    selectedFilesDiv.style.display = 'none';
}

// Configuration for batched uploads
const BATCH_SIZE = 15; // Files per batch - balance between efficiency and memory

/**
 * Handle file upload - uses batched uploads for large file sets
 */
async function handleUploadFiles(e) {
    e.preventDefault();

    if (selectedFiles.length === 0) {
        showError('Please select at least one file to upload.');
        return;
    }

    const pdfStorageMode = document.querySelector('input[name="pdf_storage"]:checked')?.value || 'none';
    const submitBtn = e.target.querySelector('button[type="submit"]');
    const csrfToken = document.querySelector('meta[name="csrf-token"]')?.getAttribute('content');
    const uploadUrl = URLBuilder.build(URLS.LIBRARY_API.COLLECTION_UPLOAD, COLLECTION_ID);

    // Decide: batch upload for large sets, single upload for small sets
    const useBatchedUpload = selectedFiles.length > BATCH_SIZE;

    try {
        submitBtn.disabled = true;
        submitBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Uploading...';

        if (useBatchedUpload) {
            // Batched upload for large file sets
            await handleBatchedUpload(selectedFiles, pdfStorageMode, csrfToken, uploadUrl);
        } else {
            // Single upload for small file sets
            await handleSingleUpload(selectedFiles, pdfStorageMode, csrfToken, uploadUrl);
        }
    } catch (error) {
        console.error('Error uploading files:', error);
        showError('Upload failed: ' + error.message);
    } finally {
        submitBtn.disabled = false;
        submitBtn.innerHTML = '<i class="fas fa-upload"></i> 🚀 Upload Files';
    }
}

/**
 * Handle batched upload - uploads files in smaller chunks
 */
async function handleBatchedUpload(files, pdfStorageMode, csrfToken, uploadUrl) {
    const totalFiles = files.length;
    const totalSize = files.reduce((sum, f) => sum + f.size, 0);
    const batches = [];

    // Split files into batches
    for (let i = 0; i < files.length; i += BATCH_SIZE) {
        batches.push(files.slice(i, i + BATCH_SIZE));
    }

    console.log(`📦 Uploading ${totalFiles} files in ${batches.length} batches`);

    // Show batched progress UI
    showBatchedProgress(files, batches.length);

    // Track results across all batches
    const allUploaded = [];
    const allErrors = [];
    let uploadedBytes = 0;

    for (let batchIndex = 0; batchIndex < batches.length; batchIndex++) {
        const batch = batches[batchIndex];
        const batchSize = batch.reduce((sum, f) => sum + f.size, 0);

        updateBatchProgress(batchIndex + 1, batches.length, uploadedBytes, totalSize);

        try {
            const result = await uploadBatch(batch, pdfStorageMode, csrfToken, uploadUrl, (loaded) => {
                // Progress within this batch
                const currentProgress = uploadedBytes + loaded;
                const percent = Math.round((currentProgress / totalSize) * 100);
                updateBatchProgressBytes(percent, currentProgress, totalSize);
            });

            if (result.uploaded) allUploaded.push(...result.uploaded);
            if (result.errors) allErrors.push(...result.errors);

            uploadedBytes += batchSize;

        } catch (error) {
            console.error(`Batch ${batchIndex + 1} failed:`, error);
            // Mark all files in failed batch as errors
            batch.forEach(file => {
                allErrors.push({ filename: file.name, error: error.message });
            });
        }
    }

    // Show combined results
    const combinedResult = {
        success: true,
        uploaded: allUploaded,
        errors: allErrors,
        summary: { successful: allUploaded.length, failed: allErrors.length }
    };

    updateProgressComplete(combinedResult);
    setTimeout(() => {
        showUploadResults(combinedResult);
        document.getElementById('upload-files-form').reset();
        selectedFiles = [];
        hideSelectedFiles();
    }, 500);
}

/**
 * Upload a single batch of files
 */
function uploadBatch(files, pdfStorageMode, csrfToken, uploadUrl, onProgress) {
    return new Promise((resolve, reject) => {
        const formData = new FormData();
        for (let file of files) {
            formData.append('files', file);
        }
        formData.append('pdf_storage', pdfStorageMode);

        const xhr = new XMLHttpRequest();

        xhr.upload.onprogress = function(e) {
            if (e.lengthComputable && onProgress) {
                onProgress(e.loaded);
            }
        };

        xhr.onload = function() {
            if (xhr.status >= 200 && xhr.status < 300) {
                try {
                    resolve(JSON.parse(xhr.responseText));
                } catch (e) {
                    reject(new Error('Invalid JSON response'));
                }
            } else {
                reject(new Error(`Server returned ${xhr.status}`));
            }
        };

        xhr.onerror = function() {
            reject(new Error('Network error'));
        };

        xhr.ontimeout = function() {
            reject(new Error('Request timeout'));
        };

        xhr.open('POST', uploadUrl);
        xhr.setRequestHeader('X-CSRFToken', csrfToken);
        xhr.timeout = 300000; // 5 min per batch
        xhr.send(formData);
    });
}

/**
 * Handle single upload (original behavior for small file sets)
 */
async function handleSingleUpload(files, pdfStorageMode, csrfToken, uploadUrl) {
    const formData = new FormData();
    for (let file of files) {
        formData.append('files', file);
    }
    formData.append('pdf_storage', pdfStorageMode);

    showCleanProgress(files);

    const data = await new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest();

        xhr.upload.onprogress = function(e) {
            if (e.lengthComputable) {
                const percentComplete = Math.round((e.loaded / e.total) * 100);
                const loadedMB = (e.loaded / (1024 * 1024)).toFixed(1);
                const totalMB = (e.total / (1024 * 1024)).toFixed(1);
                updateUploadProgress(percentComplete, loadedMB, totalMB);
            }
        };

        xhr.onload = function() {
            if (xhr.status >= 200 && xhr.status < 300) {
                try {
                    resolve(JSON.parse(xhr.responseText));
                } catch (e) {
                    reject(new Error('Invalid JSON response'));
                }
            } else {
                reject(new Error(`Server returned ${xhr.status}: ${xhr.responseText.substring(0, 100)}`));
            }
        };

        xhr.onerror = () => reject(new Error('NetworkError: Upload failed'));
        xhr.ontimeout = () => reject(new Error('Request timeout'));

        xhr.open('POST', uploadUrl);
        xhr.setRequestHeader('X-CSRFToken', csrfToken);
        xhr.timeout = 600000;
        xhr.send(formData);
    });

    if (data.success) {
        updateProgressComplete(data);
        setTimeout(() => {
            showUploadResults(data);
            document.getElementById('upload-files-form').reset();
            selectedFiles = [];
            hideSelectedFiles();
        }, 500);
    } else {
        showError(data.error || 'Upload failed');
    }
}

/**
 * Show batched upload progress UI
 */
function showBatchedProgress(files, totalBatches) {
    const progressDiv = document.getElementById('upload-progress');
    const totalFiles = files.length;
    const totalSize = files.reduce((sum, f) => sum + f.size, 0);
    const totalSizeMB = (totalSize / (1024 * 1024)).toFixed(1);

    progressDiv.innerHTML = `
        <div style="padding: 1.5rem; background: #f8f9fa; border-radius: 8px;">
            <div style="display: flex; align-items: center; justify-content: space-between; margin-bottom: 1rem;">
                <h3 style="margin: 0; font-size: 1.1rem; color: #333;">
                    <i class="fas fa-cloud-upload-alt" style="color: #667eea;"></i>
                    Uploading ${totalFiles} Files
                </h3>
                <span id="progress-summary" style="font-weight: 600; color: #667eea;">
                    0% (0 / ${totalSizeMB} MB)
                </span>
            </div>

            <div style="background: #e9ecef; border-radius: 8px; height: 12px; overflow: hidden; margin-bottom: 1rem;">
                <div id="progress-bar-fill" style="background: linear-gradient(90deg, #667eea 0%, #764ba2 100%); height: 100%; width: 0%; transition: width 0.2s ease;"></div>
            </div>

            <div id="progress-details" style="font-size: 0.9rem; color: #6c757d; margin-bottom: 0.5rem; text-align: center;">
                Preparing batched upload (${totalBatches} batches)...
            </div>

            <div id="batch-info" style="font-size: 0.85rem; color: #667eea; text-align: center;">
                Batch 1 of ${totalBatches}
            </div>
        </div>
    `;
    progressDiv.style.display = 'block';
}

/**
 * Update batched upload progress
 */
function updateBatchProgress(currentBatch, totalBatches, uploadedBytes, totalBytes) {
    const batchInfo = document.getElementById('batch-info');
    const progressDetails = document.getElementById('progress-details');

    if (batchInfo) {
        batchInfo.textContent = `Batch ${currentBatch} of ${totalBatches}`;
    }
    if (progressDetails) {
        progressDetails.textContent = `Uploading batch ${currentBatch}...`;
    }
}

/**
 * Update progress bar with byte-level accuracy
 */
function updateBatchProgressBytes(percent, uploadedBytes, totalBytes) {
    const progressBarFill = document.getElementById('progress-bar-fill');
    const progressSummary = document.getElementById('progress-summary');

    const uploadedMB = (uploadedBytes / (1024 * 1024)).toFixed(1);
    const totalMB = (totalBytes / (1024 * 1024)).toFixed(1);

    if (progressBarFill) {
        progressBarFill.style.width = percent + '%';
    }
    if (progressSummary) {
        progressSummary.textContent = `${percent}% (${uploadedMB} / ${totalMB} MB)`;
    }
}

/**
 * Show clean progress display with file list
 */
function showCleanProgress(files) {
    const progressDiv = document.getElementById('upload-progress');
    const totalFiles = files.length;
    const totalSize = files.reduce((sum, f) => sum + f.size, 0);
    const totalSizeMB = (totalSize / (1024 * 1024)).toFixed(1);

    let html = `
        <div style="padding: 1.5rem; background: #f8f9fa; border-radius: 8px;">
            <div style="display: flex; align-items: center; justify-content: space-between; margin-bottom: 1rem;">
                <h3 style="margin: 0; font-size: 1.1rem; color: #333;">
                    <i class="fas fa-cloud-upload-alt" style="color: #667eea;"></i>
                    Uploading ${totalFiles} Files
                </h3>
                <span id="progress-summary" style="font-weight: 600; color: #667eea;">
                    0% (0 / ${totalSizeMB} MB)
                </span>
            </div>

            <div style="background: #e9ecef; border-radius: 8px; height: 12px; overflow: hidden; margin-bottom: 1rem;">
                <div id="progress-bar-fill" style="background: linear-gradient(90deg, #667eea 0%, #764ba2 100%); height: 100%; width: 0%; transition: width 0.2s ease;"></div>
            </div>

            <div id="progress-details" style="font-size: 0.9rem; color: #6c757d; margin-bottom: 1rem; text-align: center;">
                Starting upload...
            </div>

            <details style="margin-top: 1rem;">
                <summary style="cursor: pointer; color: #667eea; font-weight: 500;">Show file list (${totalFiles} files)</summary>
                <div id="file-progress-list" style="max-height: 300px; overflow-y: auto; margin-top: 0.5rem;">
    `;

    files.forEach((file, index) => {
        const fileSize = (file.size / 1024 / 1024).toFixed(2);
        html += `
            <div id="file-${index}" style="display: flex; align-items: center; padding: 0.5rem; background: white; border-radius: 4px; margin-bottom: 0.25rem; border: 1px solid #dee2e6; font-size: 0.85rem;">
                <i class="fas fa-file-pdf" style="color: #e74c3c; margin-right: 0.5rem; width: 14px;"></i>
                <span style="flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">${escapeHtml(file.name)}</span>
                <span style="color: #6c757d; margin-left: 0.5rem;">${fileSize} MB</span>
            </div>
        `;
    });

    html += `
                </div>
            </details>
        </div>
    `;

    progressDiv.innerHTML = html;
    progressDiv.style.display = 'block';
}

/**
 * Update upload progress display
 */
function updateUploadProgress(percent, loadedMB, totalMB) {
    const progressBarFill = document.getElementById('progress-bar-fill');
    const progressSummary = document.getElementById('progress-summary');
    const progressDetails = document.getElementById('progress-details');

    if (progressBarFill) {
        progressBarFill.style.width = percent + '%';
    }

    if (progressSummary) {
        progressSummary.textContent = `${percent}% (${loadedMB} / ${totalMB} MB)`;
    }

    if (progressDetails) {
        if (percent < 100) {
            progressDetails.textContent = `Uploading... ${loadedMB} MB of ${totalMB} MB transferred`;
        } else {
            progressDetails.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Processing files on server...';
        }
    }
}

/**
 * Update progress to show completion
 */
function updateProgressComplete(data) {
    const progressBarFill = document.getElementById('progress-bar-fill');
    const progressSummary = document.getElementById('progress-summary');
    const progressDetails = document.getElementById('progress-details');

    if (progressBarFill) {
        progressBarFill.style.width = '100%';
        progressBarFill.style.background = '#27ae60';
    }

    // Server returns data.uploaded and data.errors (not uploaded_files/failed_files)
    const successCount = data.uploaded ? data.uploaded.length : (data.summary?.successful || 0);
    const failedCount = data.errors ? data.errors.length : (data.summary?.failed || 0);

    if (progressSummary) {
        progressSummary.textContent = `✓ ${successCount} uploaded${failedCount > 0 ? `, ${failedCount} failed` : ''}`;
        progressSummary.style.color = failedCount > 0 ? '#f39c12' : '#27ae60';
    }

    if (progressDetails) {
        progressDetails.innerHTML = `<i class="fas fa-check-circle" style="color: #27ae60;"></i> Upload complete!`;
    }
}

/**
 * Show upload results with clear breakdown
 */
function showUploadResults(data) {
    const progressDiv = document.getElementById('upload-progress');
    const resultsDiv = document.getElementById('upload-results');

    // Hide progress display
    progressDiv.style.display = 'none';

    // Server returns data.uploaded and data.errors
    const uploadedFiles = data.uploaded || [];
    const failedFiles = data.errors || [];

    // Categorize by status
    const newUploads = uploadedFiles.filter(f => f.status === 'uploaded');
    const addedToCollection = uploadedFiles.filter(f => f.status === 'added_to_collection' || f.status === 'added_to_collection_pdf_upgraded');
    const alreadyInCollection = uploadedFiles.filter(f => f.status === 'already_in_collection' || f.status === 'pdf_upgraded');

    const actualNewCount = newUploads.length + addedToCollection.length;
    const skippedCount = alreadyInCollection.length;

    let html = '<div style="padding: 1.5rem; background: #f8f9fa; border-radius: 8px;">';

    // Summary header
    html += '<h3 style="margin: 0 0 1rem 0; color: #333;"><i class="fas fa-check-circle" style="color: #28a745;"></i> Upload Complete!</h3>';

    // Stats grid
    html += '<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 1rem; margin-bottom: 1.5rem;">';

    if (actualNewCount > 0) {
        html += `<div style="background: #d4edda; padding: 1rem; border-radius: 8px; text-align: center;">
            <div style="font-size: 1.5rem; font-weight: 700; color: #155724;">${actualNewCount}</div>
            <div style="font-size: 0.85rem; color: #155724;">New Files Added</div>
        </div>`;
    }

    if (skippedCount > 0) {
        html += `<div style="background: #fff3cd; padding: 1rem; border-radius: 8px; text-align: center;">
            <div style="font-size: 1.5rem; font-weight: 700; color: #856404;">${skippedCount}</div>
            <div style="font-size: 0.85rem; color: #856404;">Already Existed</div>
        </div>`;
    }

    if (failedFiles.length > 0) {
        html += `<div style="background: #f8d7da; padding: 1rem; border-radius: 8px; text-align: center;">
            <div style="font-size: 1.5rem; font-weight: 700; color: #721c24;">${failedFiles.length}</div>
            <div style="font-size: 0.85rem; color: #721c24;">Failed</div>
        </div>`;
    }

    html += '</div>';

    // Details sections
    if (newUploads.length > 0) {
        html += `<details style="margin-bottom: 0.5rem;">
            <summary style="cursor: pointer; color: #28a745; font-weight: 500;">
                ✅ New uploads (${newUploads.length})
            </summary>
            <ul style="margin: 0.5rem 0 0 1rem; padding: 0; list-style: none;">
                ${newUploads.map(f => `<li style="padding: 0.25rem 0;">📄 ${escapeHtml(f.filename)}</li>`).join('')}
            </ul>
        </details>`;
    }

    if (addedToCollection.length > 0) {
        html += `<details style="margin-bottom: 0.5rem;">
            <summary style="cursor: pointer; color: #17a2b8; font-weight: 500;">
                📁 Added to collection (already in library) (${addedToCollection.length})
            </summary>
            <ul style="margin: 0.5rem 0 0 1rem; padding: 0; list-style: none;">
                ${addedToCollection.map(f => `<li style="padding: 0.25rem 0;">📄 ${escapeHtml(f.filename)}</li>`).join('')}
            </ul>
        </details>`;
    }

    if (alreadyInCollection.length > 0) {
        html += `<details style="margin-bottom: 0.5rem;">
            <summary style="cursor: pointer; color: #856404; font-weight: 500;">
                ⏭️ Skipped - already in collection (${alreadyInCollection.length})
            </summary>
            <ul style="margin: 0.5rem 0 0 1rem; padding: 0; list-style: none;">
                ${alreadyInCollection.map(f => `<li style="padding: 0.25rem 0;">📄 ${escapeHtml(f.filename)}</li>`).join('')}
            </ul>
        </details>`;
    }

    if (failedFiles.length > 0) {
        html += `<details open style="margin-bottom: 0.5rem;">
            <summary style="cursor: pointer; color: #dc3545; font-weight: 500;">
                ❌ Failed (${failedFiles.length})
            </summary>
            <ul style="margin: 0.5rem 0 0 1rem; padding: 0; list-style: none;">
                ${failedFiles.map(f => `<li style="padding: 0.25rem 0; color: #721c24;">📄 ${escapeHtml(f.filename)}: ${escapeHtml(f.error)}</li>`).join('')}
            </ul>
        </details>`;
    }

    // Action buttons
    html += '<div style="margin-top: 1.5rem; display: flex; gap: 1rem; flex-wrap: wrap;">';
    html += `<a href="/library/collections/${COLLECTION_ID}" class="ldr-btn-collections ldr-btn-collections-primary">
        <i class="fas fa-folder-open"></i> View Collection
    </a>`;
    html += `<button onclick="location.reload()" class="ldr-btn-collections ldr-btn-collections-secondary">
        <i class="fas fa-plus"></i> Upload More Files
    </button>`;
    html += '</div>';

    html += '</div>';

    resultsDiv.innerHTML = html;
    resultsDiv.style.display = 'block';
}

/**
 * Show error message
 */
function showError(message) {
    const progressDiv = document.getElementById('upload-progress');
    const resultsDiv = document.getElementById('upload-results');

    // Hide progress display
    if (progressDiv) {
        progressDiv.style.display = 'none';
    }

    const html = `
        <div class="alert" style="background: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; padding: 1rem; border-radius: 8px; margin-bottom: 1rem;">
            <h4 style="margin: 0 0 0.5rem 0;"><i class="fas fa-exclamation-triangle"></i> Upload Error</h4>
            <p>${message}</p>
        </div>
    `;

    resultsDiv.innerHTML = html;
    resultsDiv.style.display = 'block';
}
